<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 头部左侧 -->
        <el-tabs v-model="activeName" class="tab">
          <el-tab-pane label="销售额" name="sale" />
          <el-tab-pane label="访问量" name="active" />
        </el-tabs>
        <!-- 头部右侧 -->
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMounth">本月</span>
          <span @click="setYear">本年</span>
          <!-- 日期 -->
          <el-date-picker v-model="date" value-format="yyyy-MM-dd" size="mini" class="date" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
        </div>
      </div>
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 容器 -->
          <div ref="charts" class="charts" />
        </el-col>
        <el-col :span="6">
          <h4>门店{{ title }}排名</h4>
          <ul>
            <li>
              <span class="lindex active1">1</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
            <li>
              <span class="lindex active1">2</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
            <li>
              <span class="lindex active1">3</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
            <li>
              <span class="lindex">4</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
            <li>
              <span class="lindex">5</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
            <li>
              <span class="lindex">6</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
            <li>
              <span class="lindex">7</span>
              <span>肯德基</span>
              <span class="fright">12568</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
// 引入echarts
import * as echarts from 'echarts'
import dayjs from 'dayjs'
export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      myChart: null,
      // 收集日历信息
      date: []
    }
  },
  computed: {
    title() {
      return this.activeName === 'sale' ? '销售额' : '访问量'
    }
  },
  watch: {
    title() {
      // 重新修改图表配置
      this.myChart.setOption({
        title: {
          text: (this.activeName === 'sale' ? '销售额' : '访问量') + '趋势'
        }
      })
    }
  },
  mounted() {
    // 获取节点
    this.myChart = echarts.init(this.$refs.charts)
    this.myChart.setOption({
      title: {
        text: '销售额趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '0',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          color: 'yellowgreen',
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [10, 52, 200, 334, 390, 330, 220, 200, 334, 390, 330, 220]
        }
      ]
    })
  },
  methods: {
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMounth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style scoped>
.lindex {
  width: 18px;
  height: 18px;

  text-align: center;
  display: inline-block;
}
.active1 {
  border-radius: 50%;
  background-color: #000;
  color: aliceblue;
}
.fright {
  float: right;
}
.charts {
  width: 100%;
  height: 300px;
}
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
}
.right span {
  margin: 0 10px;
}
.date {
  width: 260px;
  margin: 0 20px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
}
h4,
ul,
li {
  margin: 0;
  padding: 0;
}
ul span {
  margin-right: 10px;
}
</style>
